package net.xo68.destiny.core.index.bplustree;

import java.util.List;

/**
 * B+ tree 节点
 * @param <K>
 * @param <V>
 * @author wuxie
 * @version 2020-12-30
 */
public class BPlusTree<K extends Comparable<K>, V> {

    // 根节点
    protected BPlusTreeNode<K, V> root;
    // 阶数，M值
    protected int order;
    // 叶子节点的链表头
    protected BPlusTreeNode<K, V> head;
    // 树高
    protected int height = 0;

    public BPlusTree(int order) {
        if (order < 3) {
            throw new IllegalArgumentException("order must be greater than 2");
        }
        this.order = order;
        this.root = new BPlusTreeNode<K, V>(true, true);
        this.head = root;
    }

    public BPlusTreeNode<K, V> getRoot() {
        return root;
    }

    public void setRoot(BPlusTreeNode<K, V> root) {
        this.root = root;
    }

    public int getOrder() {
        return order;
    }

    public BPlusTreeNode<K, V> getHead() {
        return head;
    }

    public void setHead(BPlusTreeNode<K, V> head) {
        this.head = head;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public V get(K key) {
        return root.get(key);
    }

    public V remove(K key) {
        return root.remove(key, this);
    }

    public void insertOrUpdate(K key, V value) {
        root.insertOrUpdate(key, value, this);
    }

    public void printBPlusTree() {
        this.root.printBPlusTree(0);
    }

    /**
     * 填充到列表中
     * @param idxList
     */
    public void fillToList(final List<BPlusTreeIndexEntity<K,V>> idxList){
        this.root.fillToList(idxList);
    }
}
